package com.ithuang.websocket.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ithuang.websocket.config.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ithuang.websocket.config.WebSocketServer.mes;
@ResponseBody
@RestController
public class controller {
    // ...
    @Autowired
    private  saveMessageService saveMessageService;
    @RequestMapping("/message")
    public String getMessages() {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            List<Message> messages = mes.getMessage();
            String json = objectMapper.writeValueAsString(messages);
            return json;
        } catch (JsonProcessingException e) {
            // 处理异常情况
            return "Error occurred while processing JSON data";
        }
    }

    @PostMapping("saveMessage")
    public void  saveMessage(@RequestBody Message message) {
        try {

            System.out.println(message);
            saveMessageService.saveMessage(message.getMessage(),message.getToName());
//            return ResponseEntity.ok("Message saved successfully");
        } catch (Exception e) {
//            return ResponseEntity.internalServerError().body("Error saving message: " + e.getMessage());
        }
    }
}
